/**
 * 初始化集合常量(这些常量只能由MyParamUtil初始化或获取)
 * @author 董一华
 * @since 1.00
 * @version 1.00 
 * 
 * 修订列表： v1.00 2009-10-24 
 * 			修订人：董一华 
 * 			修订内容：初始版本 
 */

package com.zjht.gmcc.bean.util;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.zjht.gmcc.bean.file.FileUtil;
import com.zjht.gmcc.bean.model.CM_FeedBackCode;
import com.zjht.gmcc.bean.model.MailList;
import com.zjht.gmcc.bean.model.ProductPara;
import com.zjht.gmcc.bean.model.Workflow;

public class InitParam {
	/**
	 * 初始化MyParam中相关常量
	 * @author dyh 2009-10-24
	 */
	public static void initMyParam() {

		// 是否
		if (MyParam.hTrueOrFalse == null)
			MyParam.hTrueOrFalse = new LinkedHashMap<String, String>();
		if (MyParam.hTrueOrFalse.size() == 0) {
			MyParam.hTrueOrFalse.put("1", "是");
			MyParam.hTrueOrFalse.put("0", "否");
		}

		// 终端公司报表中使用的月份(从2011年8月起)<2011-08,2011年8月>
		if (MyParam.hMonthZD == null)
			MyParam.hMonthZD = new LinkedHashMap<String, String>();
		if (MyParam.hMonthZD.size() == 0) {
			String startmonth = "2011-08";
			String endmonth = MyFormat.getMonth(MyFormat.getYesterday());
			MyParam.hMonthZD = MyFormat.getMonth(startmonth, endmonth);
		}

	}

	/**
	 * 初始化地市信息
	 * @author dyh 2009-10-24
	 */
	public static void initArea() {
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			MyParam.hAreaIDToAreaName.clear();
			MyParam.AreaID.clear();
			MyParam.AreaName.clear();
			MyParam.AreaShortCN.clear();
			MyParam.AreaCode.clear();
			// MyParam.hEChannelAreaIDToAreaName.clear();
			// MyParam.hMultiPlatAreaIDToAreaName.clear();
			MyParam.hZone.clear();

			String strSQL = "SELECT areaid,shortname,areaname,etype,multiplat,shortCN,areatype FROM area ORDER BY areaid";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ResultSet rs = ps.executeQuery();
			String strAreaID = "";
			String strAreaName = "";
			String strAreaShortCN = "";
			String strAreaCode = "";
			String strEType = "";
			String strMultiPlat = "";
			String areatype = "";
			while (rs.next()) {
				strAreaID = rs.getString("areaid");
				strAreaCode = rs.getString("shortname");
				strAreaName = rs.getString("areaname");
				strAreaShortCN = rs.getString("shortCN");
				strEType = rs.getString("etype");
				strMultiPlat = rs.getString("multiplat");
				areatype = rs.getString("areatype");

				if (MyConstant.AREATYPE_CITY.equals(areatype)) {
					if (!"0".equals(strAreaID)) {// 不加载“广东省”areaid=0的信息
						MyParam.hAreaIDToAreaName.put(strAreaID, strAreaName);
						MyParam.AreaID.add(strAreaID);
						MyParam.AreaCode.add(strAreaCode);
						MyParam.AreaName.add(strAreaName);
						MyParam.AreaShortCN.add(strAreaShortCN);
						// if (MyConstant.E_TYPE_GMCC.equals(strEType))// 初始化电子渠道全省统一操盘的地市信息
						// MyParam.hEChannelAreaIDToAreaName.put(strAreaID, strAreaName);
						// if (MyConstant.PLAT_MULTI_TRUE.equals(strMultiPlat))// 初始化允许一对多供货平台的地市信息
						// MyParam.hMultiPlatAreaIDToAreaName.put(strAreaID, strAreaName);
					}
				} else if (MyConstant.AREATYPE_ZONE.equals(areatype)) {
					MyParam.hZone.put(strAreaID, strAreaName);
				}
			}
			rs.close();
			rs = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
	}

	/**
	 * 初始化部门信息
	 * @author dyh 2009-11-15
	 */
	public static void initDeptName() {
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			MyParam.hDeptIDToDeptName.clear();
			MyParam.hOrgtypeDept.clear();
			String strSQL = "SELECT deptid,deptname,orgatypeid,parentdeptid FROM DEPARTMENT where status='1' ORDER BY orgatypeid,parentdeptid,deptid";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ResultSet rs = ps.executeQuery();
			String deptid = "";
			String deptname = "";
			int orgatypeid = 0, parentdeptid = 0;
			Map<String, String> hDept = null;
			while (rs.next()) {
				deptid = rs.getString("deptid");
				deptname = rs.getString("deptname");
				orgatypeid = rs.getInt("orgatypeid");
				parentdeptid = rs.getInt("parentdeptid");
				MyParam.hDeptIDToDeptName.put(deptid, deptname);
				if (parentdeptid == -1) {// 添加顶级部门
					hDept = MyParam.hOrgtypeDept.get(orgatypeid);
					if (hDept == null) {
						hDept = new LinkedHashMap<String, String>();
					}
					hDept.put(deptid, deptname);
					MyParam.hOrgtypeDept.put(orgatypeid, hDept);
				}
			}
			rs.close();
			rs = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
	}

	/**
	 * 初始化Parameter参数
	 * @author dyh 2009-10-24
	 */
	public static void initParameter() {
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			MyParam.hParameter.clear();
			MyParam.hParameterParent.clear();
			String strSQL = "SELECT ParaID,Paraname,ParentID FROM Parameter ORDER BY ParentID,listnum,ParaID";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ResultSet rs = ps.executeQuery();
			String ParaID = null;
			String Paraname = null;
			String ParentID = null;
			String ParentIDNew = null;
			LinkedHashMap<String, String> hChildPara = new LinkedHashMap<String, String>();
			while (rs.next()) {
				ParaID = rs.getString("ParaID");
				Paraname = rs.getString("Paraname");
				ParentIDNew = rs.getString("ParentID");
				MyParam.hParameter.put(ParaID, Paraname);
				if (!ParentIDNew.equals("0")) {
					if (ParentID == null) {
						ParentID = ParentIDNew;
						hChildPara.put(ParaID, Paraname);
					} else if (ParentIDNew.equals(ParentID))
						hChildPara.put(ParaID, Paraname);
					else {
						MyParam.hParameterParent.put(ParentID, hChildPara);
						hChildPara = new LinkedHashMap<String, String>();
						hChildPara.put(ParaID, Paraname);
						ParentID = ParentIDNew;
						ParentIDNew = null;
					}
				}
			}
			rs.close();
			rs = null;
			if (ParentIDNew != null && hChildPara.size() > 0) {
				MyParam.hParameterParent.put(ParentIDNew, hChildPara);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
	}

	/**
	 * 初始化组织信息
	 * @author dyh 2009-10-24
	 */
	public static void initOrgaInfo() {
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			MyParam.hOrgaCodeToAreaID.clear();
			MyParam.hOrgaCodeToOrgaTypeID.clear();
			MyParam.hOrgaCodeToShortName.clear();
			MyParam.hOrgIDToShortName.clear();
			MyParam.hOrgIDToCode.clear();
			MyParam.hOrgaCodeToParentOrgaCode.clear();
			String strSQL = " SELECT orgid,orgacode,areaid,orgatypeid,shortname,parentorgacode FROM organiza order by areaid";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ResultSet rs = ps.executeQuery();
			String strOrgaCode = "";
			String strShortName = "";
			Integer orgid = null;
			while (rs.next()) {
				orgid = Integer.valueOf(rs.getInt("orgid"));
				strOrgaCode = rs.getString("orgacode");
				strShortName = rs.getString("shortname");
				MyParam.hOrgaCodeToAreaID.put(strOrgaCode, rs.getString("areaid"));
				MyParam.hOrgaCodeToOrgaTypeID.put(strOrgaCode, rs.getInt("orgatypeid"));
				MyParam.hOrgaCodeToShortName.put(strOrgaCode, strShortName);
				MyParam.hOrgIDToShortName.put(orgid, strShortName);
				MyParam.hOrgIDToCode.put(orgid, strOrgaCode);
				MyParam.hOrgaCodeToParentOrgaCode.put(strOrgaCode, rs.getString("parentorgacode"));
			}
			rs.close();
			rs = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
	}

	/**
	 * 初始化用户菜单
	 * @author dyh 2009-10-24
	 */
	public static void initOperMenu() {
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			MyParam.hOperMenu.clear();

			String strSQL = " SELECT menuid, menuname, parentid, linkfile, helpstatus,status FROM v_sys_menu";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				MyParam.hOperMenu.put(rs.getString("menuid"), rs.getString("menuname") + "`" + rs.getString("parentid") + "`"
						+ rs.getString("linkfile") + "`" + rs.getString("helpstatus") + "`" + rs.getString("status"));
			}
			rs.close();
			rs = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
	}

	/**
	 * 获取系统定义的相关路径
	 * @author dyh 2011-02-05
	 */
	public static void initPath() {
		if (MyParam.hAjaxSQL == null) {
			MyParam.hAjaxSQL = new HashMap<String, String>();
		} else {
			MyParam.hAjaxSQL.clear();
		}
		try {
			FileUtil.read2Map(MyParam.hAjaxSQL, MyPath.FILE_AJAX_SQL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



}
